package com.wlzg.collectiontool.viewmodel

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.wlzg.collectiontool.App
import com.wlzg.collectiontool.db.AppDatabase
import com.wlzg.collectiontool.model.bean.GeneralDto
import com.wlzg.collectiontool.model.bean.LoginBean
import com.wlzg.collectiontool.model.bean.LoginRes
import com.wlzg.collectiontool.model.bean.Muser
import com.wlzg.collectiontool.net.ApiStore
import com.wlzg.collectiontool.net.CommonObserver
import com.wlzg.collectiontool.util.MConfigUtil
import com.wlzg.collectiontool.util.WifiUtil
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class LoginModel : ViewModel() {
    private var TAG = "LoginModel"
    var u: MutableLiveData<GeneralDto<String>>? = MutableLiveData()
    var succ: MutableLiveData<Int>? = MutableLiveData()
    fun loginVer() {
        ApiStore.instance.apiService?.getCode("1630032716")
            ?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe(object : CommonObserver<GeneralDto<String>>() {
                override fun onSuccess(t: GeneralDto<String>) {
                    u?.postValue(t)
                }

                override fun loginTimeOut() {

                }

            })

    }

    fun onSave(user: Muser) {
        viewModelScope.launch(Dispatchers.IO) {
            AppDatabase.getInstance().userDao().insertUser(user)
        }
    }

    fun loginOffline(loginBean: LoginBean) {
        viewModelScope.launch(Dispatchers.IO) {
            val user=Muser(loginBean.username,loginBean.password)
            val pwd = AppDatabase.getInstance().userDao().queypwd(user.account)
            if (pwd.equals(user.pwd)) {
                SPUtils.getInstance().put("Mlogin",true)
                succ?.postValue(0)
            }else{
                ToastUtils.showShort("密码错误")
            }
        }
    }

    fun mlogin(loginBean: LoginBean){
        if(WifiUtil.isNetworkConnected(App.mContext)){
            login(loginBean)
        }else loginOffline(loginBean)
    }

    fun login(loginBean: LoginBean) {
        ApiStore.instance.apiService?.login(loginBean)?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe(object : CommonObserver<GeneralDto<LoginRes>>() {
                override fun onSuccess(t: GeneralDto<LoginRes>) {
                    succ?.postValue(0)
                    SPUtils.getInstance().put("Mlogin",true)
                    onSave(Muser(loginBean.username,loginBean.password))
//                        MConfigUtil.logInRes=t.result
//                        MConfigUtil.sysAllDictItems=t.result?.sysAllDictItems
                    t.result?.let { MConfigUtil.onsave(it) }
                }

                override fun loginTimeOut() {

                }
            })

    }
}